/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gui;

import java.awt.*;
import java.awt.event.*;
import java.awt.Image.*;
import java.awt.geom.Line2D;
import java.awt.image.BufferedImage;

import java.io.*;
import java.util.ArrayList;
import javax.imageio.*;
import javax.swing.BorderFactory;
import javax.swing.JPanel;
import org.omg.PortableInterceptor.SYSTEM_EXCEPTION;

/**
 *
 * @author zhouzhao
 */
public class imagePanel extends JPanel implements MouseListener{
    
    private BufferedImage image;
    
    private BufferedImage triangle;
    private BufferedImage plus;
    private BufferedImage circle;
    
    public ArrayList<Restaurant> restDrawArray;
    public ArrayList<Photo> photoDrawArray;
    
    public int x = 0;
    public int y = 0;

    public imagePanel() {
        
        loadImage();
        restDrawArray = new ArrayList<Restaurant>();
        photoDrawArray = new ArrayList<Photo>();
        
        //register for mouse events on blank area
        addMouseListener(this);
    }
    
    @Override
    public void paint(Graphics g){
        g.drawImage(image, 0, 0, null);
        Graphics2D g2 = (Graphics2D) g;
//        Line2D line = new Line2D.Float(100, 100, 250, 250);
//        g2.drawLine(0, 0, 100, 100);
        g2.drawImage(plus, x-16, y-16, 32, 32, this);
        
        for(int i=0; i < restDrawArray.size(); i++){
            g2.drawImage(circle, (int)restDrawArray.get(i).xDim - 16, (int)restDrawArray.get(i).yDim - 16, 32, 32, this);
        }
        
        for(int j=0; j < photoDrawArray.size(); j++){
            g2.drawImage(triangle, (int)photoDrawArray.get(j).xDim - 16, (int)photoDrawArray.get(j).yDim - 16, 32, 32, this);
        }
        
        g2.setColor(Color.red);
        g2.drawString("zhouzhao", 100, 100);
    }
    
    @Override
    public void paintComponent(Graphics g){
        super.paintComponent(g);
    }
    
    public final void loadImage(){
        try {
            image = ImageIO.read(new File("la.jpg"));
            triangle = ImageIO.read(new File("triangle.png"));
            plus = ImageIO.read(new File("plus.png"));
            circle = ImageIO.read(new File("circle.png"));
        } catch (Exception e) {
            System.out.println("error: " + e.toString());
        }
    }
    
    @Override
    public Dimension getPreferredSize(){
        if(image == null){
            return new Dimension(600, 501);
        }else{
            return new Dimension(image.getWidth(), image.getHeight());
        }
    }
    
    @Override
    public void mouseClicked(MouseEvent event){
       System.out.println("mouse is clicked: x = " + event.getX() + " y = " + event.getY());
       this.x = event.getX();
       this.y = event.getY();
       
       repaint();
    }
    @Override
    public void mouseExited(MouseEvent event){
        
    }
    @Override
    public void mouseEntered(MouseEvent event){
        
    }
    @Override
    public void mouseReleased(MouseEvent event){
        
    }
    @Override
    public void mousePressed(MouseEvent event){
        
    }
}
